package com.example.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.mapper.ConsumerMapper;
import com.example.onlinemusic.model.domain.Consumer;
import com.example.onlinemusic.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    @Override
    public R allUser(IPage page) {
        // List<Consumer> consumers = (List<Consumer>) consumerMapper.selectPage(page,null);
        // return R.success("查询成功", consumers);
        return R.success("查询成功",consumerMapper.selectPage(page,null));
    }

    @Override
    public R deleteUser(int id) {
        int i = consumerMapper.deleteById(id);
        if (i > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteByIds(String[] ids) {
        // 把数组转成字符串
        StringBuilder sb = new StringBuilder();
        for (String id : ids) {
            sb.append(id + ",");
        }
        String str = sb.toString();
        // 截掉最后一个逗号
        String newId = str.substring(0, str.length() - 1);
        int result = consumerMapper.deleteByIds(newId);
        if (result > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R userDetail(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        return R.success("查询成功",consumerMapper.selectList(queryWrapper));
    }

    @Override
    public R allUser() {
        return R.success("查询成功",consumerMapper.selectList(null));
    }

}
